package com.lindroy.androidplayer.extensions

import android.view.ViewGroup
import android.widget.ImageView
import androidx.annotation.LayoutRes
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lindroid.view.EasyEditText
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.logic.network2.statecallback.ListDataState
import com.lindroy.lib_utils.extensions.getResDrawable
import com.lindroy.lib_utils.extensions.isNetworkConnect
import com.lindroy.lib_utils.extensions.shortToast
import com.lindroy.multistatelayout.MultiStateLayout
import com.lindroy.multistatelayout.widget.FrameStateLayout
import com.lindroy.multistatelayout.widget.LinearStateLayout
import com.orhanobut.logger.Logger
import com.scwang.smart.refresh.header.MaterialHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import kotlinx.android.synthetic.main.fragment_home.*

/**
 * @author Lin
 * @date 2021/4/9
 * @function
 */
open class BaseSimpleAdapter<T>(
    @LayoutRes private val layoutResId: Int,
    private val call: (holder: BaseViewHolder, item: T) -> Unit
) : BaseQuickAdapter<T, BaseViewHolder>(layoutResId) {
    override fun convert(holder: BaseViewHolder, item: T) {
        call.invoke(holder, item)
    }

}

fun RecyclerView.setLineDivider() {
    val divider = DividerItemDecoration(context, DividerItemDecoration.VERTICAL)
    divider.setDrawable(getResDrawable(R.drawable.list_divider)!!)
    addItemDecoration(divider)
}

fun MaterialHeader.initColor() {
    setColorSchemeResources(R.color.primary_dark, R.color.primary)
}
//设置字间距

fun EasyEditText.setInputLetterSpacing() {
    setEmptyChangeListener {
        letterSpacing = if (it) {
            0.05F
        } else {
            0.4F
        }
    }
}

fun Any?.isNotNull() = this != null

//region Kotlin的三目运算符
sealed class BooleanExt<out T>//起桥梁作用的中间类，定义成协变

object Otherwise : BooleanExt<Nothing>()//Nothing是所有类型的子类型，协变的类继承关系和泛型参数类型继承关系一致

class TransferData<T>(val data: T) : BooleanExt<T>()//data只涉及到了只读的操作

//声明成inline函数
inline fun <T> Boolean.yes(block: () -> T): BooleanExt<T> = when {
    this -> {
        TransferData(block.invoke())
    }
    else -> Otherwise
}

inline fun <T> BooleanExt<T>.no(block: () -> T): T = when (this) {
    is Otherwise ->
        block()
    is TransferData ->
        this.data
}
//endregion Kotlin的三目运算符


fun <T> loadListData(
    list: ListDataState<T>,
    adapter: BaseQuickAdapter<T, BaseViewHolder>,
    refreshLayout: SmartRefreshLayout,
    stateLayout: FrameStateLayout
) {
    loadListData(list, adapter, refreshLayout) {
        when (it) {
            MultiStateLayout.STATE_CONTENT -> stateLayout.showContent()
            MultiStateLayout.STATE_EMPTY -> stateLayout.showEmpty()
            MultiStateLayout.STATE_ERROR -> stateLayout.showError()
            MultiStateLayout.STATE_NO_NETWORK -> stateLayout.showNoNetwork()
        }
    }
}

fun <T> loadListData(
    list: ListDataState<T>,
    adapter: BaseQuickAdapter<T, BaseViewHolder>,
    refreshLayout: SmartRefreshLayout,
    stateLayout: LinearStateLayout
) {
    loadListData(list, adapter, refreshLayout) {
        when (it) {
            MultiStateLayout.STATE_CONTENT -> stateLayout.showContent()
            MultiStateLayout.STATE_EMPTY -> stateLayout.showEmpty()
            MultiStateLayout.STATE_ERROR -> stateLayout.showError()
            MultiStateLayout.STATE_NO_NETWORK -> stateLayout.showNoNetwork()
        }
    }
}

private fun <T> loadListData(
    list: ListDataState<T>,
    adapter: BaseQuickAdapter<T, BaseViewHolder>,
    refreshLayout: SmartRefreshLayout,
    stateCallback: (state: Int) -> Unit
) {
    list.success.yes {
        //请求成功
        when {
            list.isFirstEmpty -> {
                //第一页数据为空，显示空视图
                refreshLayout.finishRefresh()
                stateCallback.invoke(MultiStateLayout.STATE_EMPTY)
            }
            //第一页，数据不为空
            list.isRefresh -> {
                refreshLayout.finishRefresh()
                adapter.setList(list.listData)
                stateCallback.invoke(MultiStateLayout.STATE_CONTENT)
            }
            else -> {
                //加载更多
                adapter.addData(list.listData)
                refreshLayout.finishLoadMore()
                stateCallback.invoke(MultiStateLayout.STATE_CONTENT)
            }
        }
    }.no {
        //请求失败
        list.isRefresh.yes {
            //如果是第一页，直接显示错误界面
            refreshLayout.finishRefresh()
            if (adapter.data.isEmpty()) {
                //页面还有数据时不必显示缺省图
                isNetworkConnect.yes {
                    stateCallback.invoke(MultiStateLayout.STATE_ERROR)
                }.no {
                    stateCallback.invoke(MultiStateLayout.STATE_NO_NETWORK)
                }
            }
        }.no {
            //加载更多
            refreshLayout.finishLoadMore(false)
        }
    }
}

fun FrameStateLayout.setNetworkErrorLayout() {
    isNetworkConnect.yes {
        showError()
    }.no {
        showNoNetwork()
    }
}

fun LinearStateLayout.setNetworkErrorLayout() {
    isNetworkConnect.yes {
        showError()
    }.no {
        showNoNetwork()

    }
}

/**
 * 根据Url加载图片
 */
fun ImageView.loadUrl(url: String) {
    load(url) {
        placeholder(R.drawable.img_placeholder)
    }
}

/**
 * 判断是否已经登录
 */
fun judgeLogin(block: () -> Unit) {
    globalLoginInfo.isLogin.yes { block() }.no { shortToast(R.string.hint_login) }
}

/**
 * 判断当前是否处于非登录状态
 */
fun judgeNotLogin(block: () -> Unit){
   globalLoginInfo.isLogin.not().yes(block)
}

